﻿//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：EditorInspectorSuccess
// *描述：EditorInspectorSuccess
// *?2024 HDMCode . All rights reserved.
//* ======================================================================
using System;
using System.Reflection;
using HDMCode.UIFrame;
using UnityEditor;
using UnityEngine;

namespace HDMCode.UIFrameEditor
{
    public class EditorInspectorSuccess : EditorInspectorList
    {
        /// <summary>
        /// Editor重构脚本
        /// </summary>
        private UIDisplay m_Script;
        
        /// <summary>
        /// 初始化构造并将初始化值给基类
        /// </summary>
        /// <param name="script">初始化脚本</param>
        public EditorInspectorSuccess(UIDisplay script) : base(script) => m_Script = script;
        
        /// <summary>
        /// 绘制关联脚本
        /// </summary>
        public void DrawRelevance()
        {
            var com = m_Script.GetComponent($"{m_Script.uISingleName}");
            if (!com)
                UnityEditor.EditorGUILayout.HelpBox($"自动添加生命脚本失败，需要手动添加 {m_Script.uISingleName} 脚本才能生效！", MessageType.Error);
            else
                com.hideFlags = HideFlags.HideInInspector;
        }

        /// <summary>
        /// 设置音频选项
        /// </summary>
        public void DrawSetAudio()
        {
            using (new GUILayout.HorizontalScope())
            {
                Texture icon = EditorGUIUtility.IconContent("d_Profiler.Audio").image;
                GUILayout.Label(icon, GUILayout.Width(20));
                GUILayout.Label("开启音效", GUILayout.ExpandWidth(true));
                GUILayout.Space(60);
                m_Script.openAudio = (AudioClip)UnityEditor.EditorGUILayout.ObjectField(m_Script.openAudio, typeof(AudioClip), true, GUILayout.MaxWidth(800), GUILayout.MinWidth(10));
            }
            using (new GUILayout.HorizontalScope())
            {
                Texture icon = EditorGUIUtility.IconContent("d_Profiler.Audio").image;
                GUILayout.Label(icon, GUILayout.Width(20));
                GUILayout.Label("关闭音效", GUILayout.ExpandWidth(true));
                GUILayout.Space(60);
                m_Script.closeAudio = (AudioClip)UnityEditor.EditorGUILayout.ObjectField(m_Script.closeAudio, typeof(AudioClip), true, GUILayout.MaxWidth(800), GUILayout.MinWidth(10));
            }
        }

        /// <summary>
        /// 绘制Xlua支持选项
        /// </summary>
        [Obsolete("从框架V2开始将放弃使用Xlua作为热更新方案，UI框架将不在支持使用XLau作为启动！")]
        public void DrawSupportXlua()
        {
            GUILayout.BeginHorizontal();
            {
                Texture icon = EditorGUIUtility.IconContent("BuildSettings.Editor.Small").image;
                GUILayout.Label(icon, GUILayout.Width(20));
                GUILayout.Label("设置平台", GUILayout.ExpandWidth(true));
                GUILayout.Space(60);
                m_Script.compilationType = GUILayout.SelectionGrid(m_Script.compilationType, new[] { "C#", "Xlua" }, 2, GUILayout.MaxWidth(800), GUILayout.MinWidth(10));
                GUILayout.EndHorizontal();

                if (m_Script.compilationType == 1)
                {
                    if (EditorUtility.DisplayDialog("提示", "从该框架1.0版本开始，已停止使用Xlua作为热更新方案！", "好的"))
                    {
                        m_Script.compilationType = 0;
                    }
                }
            }
        }

        /// <summary>
        /// 绘制资源名称
        /// </summary>
        public void DrawAssetsName()
        {
            using (new UnityEditor.EditorGUILayout.HorizontalScope()) 
            {
                EditorGUI.BeginDisabledGroup(true);
                Texture tileIcon = EditorGUIUtility.IconContent("cs Script Icon").image;
                GUILayout.Label(tileIcon, GUILayout.Width(20), GUILayout.Height(18));
                m_Script.uISingleName = UnityEditor.EditorGUILayout.TextField("编译名称：", m_Script.uISingleName , GUILayout.Height(20));
                EditorGUI.EndDisabledGroup();
            }
        }
        
        /// <summary>
        /// 设置动画状态机选项
        /// </summary>
        public void DrawSetAnimation()
        {
            using (new GUILayout.HorizontalScope())
            {
                Texture icon = EditorGUIUtility.IconContent("PreTextureRGB").image;
                GUILayout.Label(icon, GUILayout.Width(20));
                GUILayout.Label("设置动画", GUILayout.ExpandWidth(true));
                GUILayout.Space(60);
                m_Script.animation =(RuntimeAnimatorController)UnityEditor.EditorGUILayout.ObjectField(m_Script.animation, typeof(RuntimeAnimatorController), true, GUILayout.MaxWidth(800), GUILayout.MinWidth(10));
            }
        }

        /// <summary>
        /// 绘制资源描述
        /// </summary>
        public void DrawAssetsDescription()
        {
            using (new UnityEditor.EditorGUILayout.HorizontalScope()) 
            {
                EditorGUI.BeginDisabledGroup(true);
                Texture tileIcon = EditorGUIUtility.IconContent("Clipboard").image;
                GUILayout.Label(tileIcon, GUILayout.Width(20), GUILayout.Height(18));
                m_Script.scriptDescribe = UnityEditor.EditorGUILayout.TextField("资源描述：", m_Script.scriptDescribe , GUILayout.Height(20));
                EditorGUI.EndDisabledGroup();
            }
            UnityEditor.EditorGUILayout.Space();
        }

        /// <summary>
        /// 设置背景颜色选项
        /// </summary>
        public void DrawSetBackGroundColor()
        {
            if (m_Script.wrapperType== WrapperType.Fixation || m_Script.wrapperType== WrapperType.inform)
                return;
            
            if (m_Script.backGroundType == BackGroundType.Custom)
            {
                GUILayout.BeginHorizontal();
                Texture icon = EditorGUIUtility.IconContent("PreTextureRGB").image;
                GUILayout.Label(icon, GUILayout.Width(20));
                GUILayout.Label("设置颜色", GUILayout.ExpandWidth(true));
                GUILayout.Space(60);
                m_Script.backGroundColor = UnityEditor.EditorGUILayout.ColorField(m_Script.backGroundColor, GUILayout.MaxWidth(800), GUILayout.MinWidth(10));
                GUILayout.EndHorizontal();
            }
        }

        /// <summary>
        /// 绘制UI窗口方式选项
        /// </summary>
        public void DrawWrapper()=> ReadEumeAttribute("窗口设置", m_Script.wrapperType, (value) => { m_Script.wrapperType = (WrapperType)Enum.Parse(m_Script.wrapperType.GetType(), (string)value); });

        /// <summary>
        /// 绘制UI显示方式选项
        /// </summary>
        public void DrawShow()=> ReadEumeAttribute("显示设置", m_Script.showType, (value) => { m_Script.showType = (ShowType)Enum.Parse(m_Script.showType.GetType(), (string)value); });

        /// <summary>
        /// 绘制UI穿透方式选项
        /// </summary>
        public void DrawPierce()
        {
            if (m_Script.wrapperType== WrapperType.Fixation || m_Script.wrapperType== WrapperType.inform)
                return;
            ReadEumeAttribute("穿透设置", m_Script.pierceType, (value) => { m_Script.pierceType = (PierceType)Enum.Parse(m_Script.pierceType.GetType(), (string)value); });
        }

        /// <summary>
        /// 绘制UI背景方式选项
        /// </summary>
        public void DrawBackGround()
        {
            if (m_Script.wrapperType== WrapperType.Fixation || m_Script.wrapperType== WrapperType.inform)
                return;
            ReadEumeAttribute("背景设置", m_Script.backGroundType, (value) => { m_Script.backGroundType = (BackGroundType)Enum.Parse(m_Script.backGroundType.GetType(), (string)value); });
        }

        /// <summary>
        /// 通过特性映射枚举选项
        /// </summary>
        /// <param name="lableName">需要显示的菜单名称</param>
        /// <param name="inputEume">绑定的枚举</param>
        /// <param name="action">选中回调</param>
        /// <param name="iconName">菜单图标</param>
        /// <typeparam name="T">枚举</typeparam>
        public void ReadEumeAttribute<T>(string lableName, T inputEume, GenericMenu.MenuFunction2 action, string iconName = "d_PreMatLight0") where T : struct, Enum
        {
            GUILayout.BeginHorizontal();
            Texture icon = EditorGUIUtility.IconContent(iconName).image;
            GUILayout.Label(icon, GUILayout.Width(20));
            GUILayout.Label(lableName, GUILayout.ExpandWidth(true));
            GUILayout.Space(60);
            var selected = inputEume.GetType().GetField(inputEume.ToString());


#if (UNITY_2022)
            HDMEnumChildLabelAttribute selectedAtt = (HDMEnumChildLabelAttribute)selected.GetAttribute(typeof(HDMEnumChildLabelAttribute)); 
#else
            HDMEnumChildLabelAttribute selectedAtt = (HDMEnumChildLabelAttribute)selected.GetCustomAttribute(typeof(HDMEnumChildLabelAttribute));
#endif
            if (GUILayout.Button(selectedAtt.name, GUILayout.MaxWidth(800), GUILayout.MinWidth(10)))
            {
                GenericMenu menu = new GenericMenu();
                foreach (var item in Enum.GetNames(inputEume.GetType()))
                {
                    var eumeField = inputEume.GetType().GetField(item);
#if (UNITY_2022)
                    HDMEnumChildLabelAttribute childAtt = (HDMEnumChildLabelAttribute)eumeField.GetAttribute(typeof(HDMEnumChildLabelAttribute));
#else
                    HDMEnumChildLabelAttribute childAtt = (HDMEnumChildLabelAttribute)eumeField.GetCustomAttribute(typeof(HDMEnumChildLabelAttribute));
#endif
                    if (selectedAtt.name == childAtt.name)
                        menu.AddItem(new GUIContent(childAtt.name), true, action, item);
                    else
                        menu.AddItem(new GUIContent(childAtt.name), false, action, item);
                }
                menu.ShowAsContext();
            }

            GUILayout.EndHorizontal();
        }
        
        
    }
}
